package lz.controls.text
{

	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	import flashx.textLayout.events.ScrollEvent;
	
	import lz.controls.ScrollBar;
	import lz.controls.ScrollBarDirection;
	import lz.controls.containers.BaseScrollPane;
	import lz.controls.containers.ScrollPane;
	import lz.controls.core.InvalidationType;

	/**
	 *富文本 
	 * @author lake
	 * 
	 */	
	public class RichTxt extends ScrollPane
	{
		/**
		 * 记录
		 */		
		protected var _records						:Array;
		/**
		 * 最大行数
		 */		
		protected var _maxLines					:int=10;
		/**
		 * 表情dic
		 */		
		protected var _faceDic						:Dictionary;
		/**
		 * 垂直滚动位置
		 */		
		protected var _verticalScrollPosition		:Number=0;
		/**
		 * 样式
		 */		
		private static var defaultStyles			:Object={};
		/**
		 * 滤镜
		 */		
		private var _filters						:Array=[];
		/**
		 * 缩进
		 */		
		private var _leading						:int=0;

		public static function getStyleDefinition():Object
		{
			return mergeStyles(defaultStyles, BaseScrollPane.getStyleDefinition());
		}

		/**
		 * 	var dic:Dictionary = new Dictionary();
			dic["/SYS"] = "ChatChannelFlagSystem";
			dic["/SER"] = "ChatChannelFlagService";
			dic["/RUM"] = "ChatChannelFlagRumour";
		 * @param width
		 * @param faceDic
		 *
		 */
		public function RichTxt(width:int=200, faceDic:Dictionary=null)
		{
			super();
			super.width = width;
			_faceDic=(faceDic) ? faceDic : new Dictionary();
		}
		/**
		 * 设置表情数据 
		 * @param faceDic
		 * 
		 */
		public function setFaceDic(faceDic:Dictionary):void
		{
			_faceDic=(faceDic) ? faceDic : new Dictionary();
		}
		/**
		 *配置ui 
		 * 
		 */
		override protected function configUI():void
		{
			super.configUI();
		}
		/**
		 *绘制布局 
		 * 
		 */
		override protected function drawLayout():void
		{
			super.drawLayout();
		}
		/**
		 *添加内容 
		 * @param value
		 * 
		 */
		public function appendHtmlText(value:String):void
		{
			addLine(value);
		}
		/**
		 *获取行数 
		 * @return 
		 * 
		 */
		public function get lines():int
		{
			if (_records)
			{
				return _records.length;
			}
			return 0;
		}
		/**
		 *更新 
		 * 
		 */
		override public function update():void
		{
			super.update();
			if (_records && _records.length)
			{
				var len:int=_records.length;
				for (var i:int=0; i < len; i++)
				{
					if (i == 0)
					{
						_records[i].y=0;
					}
					else
					{
						_records[i].y=_records[i - 1].y + _records[i - 1].height + _leading;
					}
				}
				contentHeight=_records[len - 1].y + _records[len - 1].height;
			}
			else
			{
				contentHeight=0;
			}
			invalidate(InvalidationType.SIZE);
		}
		/**
		 *获取设置缩进数 
		 * @return 
		 * 
		 */
		public function set leading(value:int):void
		{
			if (_leading != value)
			{
				_leading=value;
				update();
			}
		}
		public function get leading():int
		{
			return _leading;
		}
		/**
		 *获取设置最大行数 
		 * @return 
		 * 
		 */
		public function get maxLines():int
		{
			return _maxLines;
		}
		public function set maxLines(value:int):void
		{
			if (_maxLines != value)
			{
				_maxLines=value;
				if (_records)
				{
					var len:int=_records.length;
					while (_records.length > value)
					{
						var lineTxt:LineTxt=_records.shift();
						if (lineTxt && lineTxt.parent)
						{
							lineTxt.parent.removeChild(lineTxt);
							lineTxt.destroy();
						}
						lineTxt=null;
					}
					if (len != _records.length)
					{
						update();
					}
				}
			}
		}
		/**
		 *删除第一行 
		 * 
		 */
		public function deleteFirstLine():void
		{
			if (_records)
			{
				var lineTxt:LineTxt=_records.shift();
				if (lineTxt && lineTxt.parent)
				{
					lineTxt.parent.removeChild(lineTxt);
					lineTxt.destroy();
				}
				lineTxt=null;
				update();
			}
		}
		/**
		 *删除最后行 
		 * 
		 */
		public function deleteLastLine():void
		{
			if (_records)
			{
				var lineTxt:LineTxt=_records.pop();
				if (lineTxt && lineTxt.parent)
				{
					lineTxt.parent.removeChild(lineTxt);
					lineTxt.destroy();
				}
				lineTxt=null;
				update();
			}
		}
		/**
		 *删除行 
		 * @param index
		 * @param numLines
		 * 
		 */
		public function deleteLine(index:int, numLines:int=1):void
		{
			if (_records)
			{
				var temp:Array=_records.splice(index, numLines);
				while (temp.length > 0)
				{
					var lineTxt:LineTxt=temp.shift();
					if (lineTxt && lineTxt.parent)
					{
						lineTxt.parent.removeChild(lineTxt);
						lineTxt.destroy();
					}
					lineTxt=null;
				}
				temp=null;
				update();
			}
		}
		/**
		 *清理 
		 * 
		 */
		public function clear():void
		{
			if (_records)
			{
				while (_records.length > 0)
				{
					var lineTxt:LineTxt=_records.shift();
					if (lineTxt)
					{
						if (lineTxt.parent)
							lineTxt.parent.removeChild(lineTxt);
						lineTxt.destroy();
					}
					lineTxt=null;
				}
			}
			_records=null;
			update();
		}
		/**
		 *销毁 
		 * 
		 */
		override public function destroy():void
		{
			super.destroy();
			clear();
			_faceDic=null;
		}
		/**
		 *使用滤镜 
		 * @param arr
		 * 
		 */
		public function useFilter(arr:Array):void
		{
			if (arr == null)
				arr=[];
			_filters=arr;
			if (_records == null || _records.length == 0)
				return;
			for each (var line:LineTxt in _records)
			{
				if (line)
					line.useFilter(_filters);
			}
		}
		/**
		 *添加一行 
		 * @param msg
		 * @param index
		 * 
		 */
		public function addLine(msg:String, index:int=-1):void
		{
			if (_records == null)
			{
				_records=[];
			}
			if (_records.length > _maxLines)
			{
				deleteFirstLine();
			}
			var temp:int =  maxVerticalScrollPosition;
			var lineTxt:LineTxt=new LineTxt(width - ScrollBar.WIDTH, _faceDic);
			lineTxt.useFilter(_filters);
			lineTxt.text=msg;
			lineTxt.x=5; // modify
			contentClip.addChild(lineTxt);
			if (index == -1)
				index=_records.length;
			_records.splice(index, 0, lineTxt);
			
			update();
			if(verticalScrollPosition == temp)
			{
				verticalScrollPosition = maxVerticalScrollPosition;
			}
		}
	}
}
